<template>
  <div class="card-container">
    <el-form :model="form" :rules="formRules" ref="formRef" label-width="120px" style="width: 100%;" label-position="right">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>添加角色</span>
        </div>
        <el-form-item label="角色名称：" prop="title">
          <el-input v-model="form.title"></el-input>
          <!-- <div class="tips">后台显示的权限名称</div> -->
        </el-form-item>
        <el-form-item label="角色描述：" prop="desc">
          <el-input v-model="form.desc"></el-input>
          <!-- <div class="tips">后台显示的权限名称</div> -->
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="包含权限：" prop="content">
          <div v-for="item in permissionList" :key="item.id" style="margin-top: 10px;">
            <el-checkbox :indeterminate="item.isIndeterminate" v-model="item.checkAll" @change="itemChange(item, $event)">{{ item.title }}</el-checkbox>
            <div style="margin: 15px 30px;">
              <el-checkbox-group v-model="item.checkList" @change="sitemChange(item, $event)">
                <el-checkbox v-for="sitem in item.children" :label="sitem.id" :key="sitem.id">{{ sitem.title }}</el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即添加</el-button>
        <!-- <el-button type="primary" @click="handleChecked">处理已选中</el-button> -->
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getPermissionList, addRole } from '@/api/permission'
export default {
  name: 'addRole',
  data() {
    return {
      form: {
        title: '',
        sort: '1',
        content: [],
      },
      formRules: {
        title: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入角色排序', trigger: 'blur' }],
        route: [{ required: true, message: '请选择包含路由', trigger: 'blur' }],
      },
      permissionList: [],
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()
      if (res.status != 1) return this.$message.error(res.msg)
      console.log(res)
      res.data.forEach(x => {
        this.$set(x, 'checkAll', false)
        this.$set(x, 'checkList', [])
        this.$set(x, 'isIndeterminate', false)
      })
      this.permissionList = res.data
    },
    itemChange(item, val) {
      console.log(item)
      console.log(val)
      let cids = []
      item.children.forEach(x => {
        cids.push(x.id)
      })
      item.checkList = val ? cids : []
      item.isIndeterminate = false
    },
    sitemChange(item, val) {
      console.log(item)
      console.log(val)
      let checkedCount = val.length
      item.checkAll = checkedCount === item.children.length
      item.isIndeterminate = checkedCount > 0 && checkedCount < item.children.length
    },
    handleChecked() {
      let checkList = []
      this.permissionList.forEach((x, i) => {
        if (x.checkList.length != 0) {
          let obj = {}
          obj.fid = x.id
          obj.children = []
          for (var k = 0; k < x.checkList.length; k++) {
            obj.children[k] = {}
            obj.children[k].tid = x.checkList[k]
          }
          checkList.push(obj)
        }
      })
      this.form.content = checkList
      console.log(this.form.content)
    },
    async submitForm() {
      this.handleChecked()
      if (this.form.content.length == 0) {
        return this.$message.warning('请先选择角色权限')
      }
      this.$refs.formRef.validate(async valid => {
        if (!valid) return false
        const res = await addRole(this.form)
        if (res.status != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
    },
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/rolesList')
    },
  },
}
</script>

<style lang="scss" scoped>
// .el-form-item {
//   // width: 420px;
// }
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  width: 420px;
}
::v-deep .el-checkbox {
  height: auto !important;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  padding-right: 12px;
}
::v-deep .el-range-separator {
  line-height: 28px;
}
::v-deep .el-range-input {
  cursor: pointer;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  width: 600px;
}
.tips {
  color: #999;
  margin-top: 10px;
  font-size: 12px;
}
</style>
